//#include "AnaDef.h"
//#include "XGLUtils.h"
#include "style.h"

const Double_t mass[]={0.938272, 0.139570, 0.493677, 5.109989e-04 };
const TString types[]={"p","#pi","K","e"};
const char* vars[]={"A","#mu", "#sigma"};
const Int_t col[]={kBlue, kRed, kGreen+3, kMagenta};
const Int_t sty[]={20,29,22,21};

//test->
const Int_t ntype = 4;
//test<-

TGraphAsymmErrors * getGr(const Int_t itype, const Int_t ipar, const Int_t gg, TList * ll)
{
  TCanvas *c1=new TCanvas;
  
  PadSetup(c1);
  c1->SetLeftMargin(0.14);
  c1->SetRightMargin(0.03);

  gPad->SetLogx();

  //--------------------------------------------
  const TString fin="outmn2d.root";
  TFile::Open(fin);
  //<10 ? to make no err getting
  TGraphAsymmErrors* gfit=XGLUtils::TreeToGraph("mn2d","",(Char_t*)"xx", ipar<0? Form("y%d",itype) : Form("p%d%d",itype, ipar),(Char_t*)"",(Char_t*)"", ipar<10? (Char_t*)"":  Form("le%d%d",itype, ipar), ipar<10? (Char_t*)"":  Form("he%d%d",itype, ipar),1);
  gfit->SetName(Form("fit%s",gfit->GetName()));
  ll->Add(gfit);

  if(gg){
    for (Int_t i=0; i<gfit->GetN();i++){
      gfit->GetX()[i]/=mass[itype];
    }
  }
  //--------------------------------------------
  TFile::Open("MCtrue_save.root");
  const TString pwd=gSystem->pwd();
  TGraphAsymmErrors * gtrue=XGLUtils::TreeToGraph("mn2d","", (Char_t*)"xx", ipar<0? Form("y%d",itype) : Form("p%d%d",itype, ipar),(Char_t*)"",(Char_t*)"", ipar<10? (Char_t*)"":  Form("le%d%d",itype, ipar), ipar<10? (Char_t*)"":  Form("he%d%d",itype, ipar),1);
  gtrue->SetName(Form("true%s",gtrue->GetName()));

  gtrue->SetMarkerColor(col[itype]);
  gtrue->SetMarkerStyle(sty[itype]);
  ResetStyle(gtrue);
  ll->Add(gtrue);
  if(gg){
    for (Int_t i=0; i<gtrue->GetN();i++){
      gtrue->GetX()[i]/=mass[itype];
    }
  }
  //--------------------------------------------
  if(ipar==0)
    gfit->GetYaxis()->SetRangeUser(0,1);
  else if(ipar==1){
    gfit->GetYaxis()->SetRangeUser(30,120);
    if(itype==3){
      gfit->GetYaxis()->SetRangeUser(70,85);
      gPad->SetGrid();
    }
  }
  else if(ipar==2)
    gfit->GetYaxis()->SetRangeUser(0,0.1);

  gfit->GetYaxis()->SetTitle(Form("%s", ipar<0? "N" : vars[ipar]));
  gfit->GetXaxis()->SetTitle(gg?"#beta#gamma":"p (GeV/c)");
  
  TLegend *tmplg=new TLegend(0.5,0.7,0.8,0.9);
  tmplg->SetHeader(types[itype]);
  ResetStyle(tmplg);

  gfit->Draw("alp");
  gtrue->Draw("lp");
  tmplg->AddEntry(gfit, "fit", "l");  
  tmplg->AddEntry(gtrue, "MC true", "p");
  tmplg->Draw();

  c1->Print(Form("varType%dIpar%dgg%d.eps", itype, ipar, gg));
  
  //--------------------------------------------
  
  TGraphAsymmErrors *gratio=new TGraphAsymmErrors(*gtrue);
  for (Int_t i=0; i<gratio->GetN();i++){
    if(gfit->GetY()[i]<1e-6){
      gratio->GetY()[i] = 0;
      gratio->GetEYlow()[i] = 0;
    }
    else{
      gratio->GetY()[i]/=gfit->GetY()[i]; 
      gratio->GetY()[i]-=1; 
      
      gratio->GetEYlow()[i] /=gfit->GetY()[i]; 
      gratio->GetEYhigh()[i]/=gfit->GetY()[i]; 
    }
  }

  gratio->SetName(Form("ratio%s",gratio->GetName()));

  if(ipar==0)
    gratio->GetYaxis()->SetRangeUser(-0.5,0.5);
  else if(ipar==1)
    gratio->GetYaxis()->SetRangeUser(-0.02,0.02);
  else if(ipar==2)
    gratio->GetYaxis()->SetRangeUser(-0.5,0.5);
  else
    gratio->GetYaxis()->SetRangeUser(-0.5,0.5);

  if(gg)
    gratio->GetXaxis()->SetLimits(0.5,400);

  gratio->GetYaxis()->SetTitle(Form("%s_{true}/%s_{fit}-1", ipar<0? "N" : vars[ipar], ipar<0 ? "N" : vars[ipar]));
  gratio->GetXaxis()->SetTitle(gg?"#beta#gamma":"p (GeV/c)");
  
  ll->Add(gratio);

  return gratio;
}

TList * paraRatio(const Int_t ipar, const Int_t gg = 0)
{
  SetGlobalStyle(0);
  
  TList *ll = new TList;

  TGraphAsymmErrors * grs[4];
  for(Int_t itype=0; itype<ntype; itype++){
    grs[itype] = getGr(itype, ipar, gg,ll); 
  }
  
  TCanvas *c1=new TCanvas;
  
  PadSetup(c1);
  c1->SetLeftMargin(0.14);
  c1->SetRightMargin(0.03);

  gPad->SetGrid();

  TLegend *lg=new TLegend(0.3,0.2,0.7,0.35);
  ResetStyle(lg);
  
  for(Int_t itype=0; itype<ntype; itype++){
    grs[itype]->Draw(itype?"lp":"alp");
    lg->AddEntry(grs[itype],types[itype],"lp");
  }
  
  gPad->SetLogx();

  TF1 * f1=new TF1("f1","0",0,2000);
  f1->SetLineColor(kBlack);
  f1->Draw("same");

  lg->Draw();

  c1->Print(Form("meanRatio-p%d-%d.eps",ipar,gg));

  return ll;
}

int main()
{
  return 0;
}

/*
 .L ~/.task/2dbetabloch/paraRatio.C
 ll=paraRatio(1,1)
gr0=(TGraphAsymmErrors*) ll->FindObject("truegrmn2dp01")
gr1=(TGraphAsymmErrors*) ll->FindObject("truegrmn2dp11")
gr2=(TGraphAsymmErrors*) ll->FindObject("truegrmn2dp21")
new TCanvas
gPad->SetLogx()

gr0->Draw("apl")

TF1 * f3=new TF1("f3",XGLUtils::HadronBetheBloch,0.5,1000,5)
const Double_t tmp[]={3.47839e+01, 2.17599e+00, 2.20851e-01, -4.02987e-01, 7.71210e+01 };
f3->SetParameters(tmp);
gr0->Fit(f3)

TF1 * f1=new TF1("f1",XGLUtils::Lund,0.5,1000,5)
const Double_t tmp[]={2.893149e+01, 8.099705e+00/2.893149e+01, 3.443490e+00, 1.179705e+00, 7.575471e+01};
f1->SetParameters(tmp);
gr0->Fit(f1)

TF1 * f2=new TF1("f2",XGLUtils::RawAleph,0.5,1000,5)
const Double_t tmp[]={4.401269, 9.725370, 0.000178, 1.904962, 1.426576};
f2->SetParameters(tmp);
gr0->Fit(f2)


fr0=(TGraphAsymmErrors*) ll->FindObject("fitgrmn2dp01")
fr0->Draw("pl")

gr1->Draw("pl")
gr2->Draw("pl")

pp0=(TGraphAsymmErrors*) ll->FindObject("fitgrmn2dp01")
pp0->Draw("pl")
pp0->Draw("apl")
gr0->Draw("pl")
gr1->Draw("pl")
gr2->Draw("pl")
pp0->Draw("apl")
gr0->Draw("pl")
gr1->Draw("pl")
gr2->Draw("pl")
new TCanvas
 gr0->Draw("pl")
 gr0->Draw("pal")

*/
